package com.etennis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etennis.constants.AppHttpCodeEnum;
import com.etennis.exception.SystemException;
import com.etennis.mapper.CommodityMapper;
import com.etennis.mapper.MemberMapper;
import com.etennis.mapper.OrderMapper;
import com.etennis.pojos.entity.Commodity;
import com.etennis.pojos.entity.Member;
import com.etennis.pojos.entity.Order;
import com.etennis.pojos.vo.app.CommodityVO;
import com.etennis.pojos.vo.app.ExchangeVO;
import com.etennis.pojos.vo.app.OrderVO;
import com.etennis.service.IMemberService;
import com.etennis.service.IOrderService;
import com.etennis.utils.BeanCopyUtils;
import com.etennis.utils.RandomUtil;
import com.etennis.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    CommodityMapper commodityMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    MemberMapper memberMapper;

    @Override
    public List<CommodityVO> commodity() {
        List<Commodity> commodities = commodityMapper.selectList(new LambdaQueryWrapper<>());
        List<CommodityVO> commodityVOList = commodities.stream().map(each -> {
            return BeanCopyUtils.copyBean(each, CommodityVO.class);
        }).collect(Collectors.toList());
        return commodityVOList;
    }

    @Override
    public List<OrderVO> orderList() {
        Long userId= SecurityUtils.getLoginUserId();
        List<Order> orders = orderMapper.selectList(new LambdaQueryWrapper<Order>().eq(Order::getMemberId, userId));
        List<OrderVO> orderVOList = orders.stream().map(each -> {
            OrderVO orderVO = BeanCopyUtils.copyBean(each, OrderVO.class);
            Commodity commodity = commodityMapper.selectById(each.getCommodityId());
            orderVO.setName(commodity.getName());
            orderVO.setUrl(commodity.getUrl());
            orderVO.setPrice(commodity.getPrice());
            return orderVO;
        }).collect(Collectors.toList());
        return orderVOList;
    }

    /**
     * 插入新order

     * @return
     */
    @Override
    public ExchangeVO exchange(Long commodityId,Long memberId) {
        Commodity commodity = commodityMapper.selectById(commodityId);
        // 用户扣减积分
        Member member = memberMapper.selectOne(Wrappers.lambdaQuery(Member.class).eq(Member::getId, memberId));
        member.setRestScore(member.getRestScore()-commodity.getPrice());
        memberMapper.updateById(member);
        if(commodity.getRestNum()>0){
        commodity.setRestNum(commodity.getRestNum()-1);
        commodityMapper.updateById(commodity);
        }
        else throw new SystemException(AppHttpCodeEnum.NOT_ENOUGH);
        Order order=new Order();
        order.setMemberId(memberId);
        order.setCommodityId(commodityId);
        LocalDate currentDate = LocalDate.now();
        LocalDate newDate = currentDate.plusDays(90);
        Date date = Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        order.setDeadline(date);
        String code= RandomUtil.generateCode();
        order.setCode(code);
        order.setFlag("1");
        orderMapper.insert(order);
        return new ExchangeVO(code);
    }
}
